package com.litong.litongjiaoyu.controller;

import com.litong.litongjiaoyu.annotation.IgnoreAuth;
import com.litong.litongjiaoyu.entity.StuHomework;
import com.litong.litongjiaoyu.service.HomeworkService;
import com.litong.litongjiaoyu.utils.JSONResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName HomeWorkController
 * @Description 作业控制层
 * @Author jingz
 * @Date 2019/11/29 10:05
 * @Version 1.0
 **/
@Log4j2
@RestController
@RequestMapping("/homework")
public class HomeWorkController {
    @Autowired
    private HomeworkService homeworkService;

    @IgnoreAuth
    @ResponseBody
    @GetMapping("/getnumber")
    public JSONResult getNumber(String className, String campus){
        return JSONResult.ok(homeworkService.getNumber(className, campus));
    }

    @ResponseBody
    @PostMapping("/insert")
    public JSONResult insert(@RequestBody StuHomework stuHomework){
        log.info("作业内容添加");
        try {
            if (homeworkService.insertHomework(stuHomework) > 0) {
                log.info("添加成功");

                return JSONResult.ok();
            } else {
                log.error("添加失败，错误信息如上");
                return JSONResult.errorMsg("fail");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return JSONResult.errorException(e.getMessage());
        }
    }

    @IgnoreAuth
    @ResponseBody
    @GetMapping("/select")
    public JSONResult select(String className, String campus) {
        log.info("开始获取作业内容");

        List<StuHomework> stuHomework = homeworkService.queryHomework(className, campus);
        if (stuHomework != null) {
            log.info("获取成功：" + stuHomework.toString());

            return JSONResult.ok(stuHomework);
        } else {
            log.warn("获取失败：原因：1、可能没有该数据；2、可能出现错误，具体查看日志");
            return JSONResult.errorMsg("fail");
        }
    }
}
